/**
 * @author renato
 * Created on 04/11/2005
 *
 */
package br.unb.statistic.distribution;

import java.util.HashMap;

import br.unb.plugins.geral.model.GeneratorModel;
import br.unb.statistic.InterPacketTimeGenerator;
import br.unb.statistic.RandomGenerator;

public class BurstGenerator implements InterPacketTimeGenerator {

    private InterPacketTimeGenerator pauseGenerator;
    private RandomGenerator offGenerator, onGenerator;
    private double onTime, onTimeCount;

    public BurstGenerator() {
    }

    public BurstGenerator(RandomGenerator offPeriodGenerator, RandomGenerator onPeriodGenerator, InterPacketTimeGenerator interPacketTimeGenerator) {

        offGenerator = offPeriodGenerator;
        onGenerator = onPeriodGenerator;
        pauseGenerator = interPacketTimeGenerator;
    }

    /* (non-Javadoc)
     * @see br.com.statistic.RandomGenerator#inverseCdf(double)
     */
    public double nextInterPacketTime() {

        double pauseTime;
        if (onTimeCount < onTime) {

            pauseTime = this.pauseGenerator.nextInterPacketTime();
            this.onTimeCount += pauseTime;

        } else {

            this.onTime = this.onGenerator.rand();
            this.onTimeCount = 0;
            pauseTime = this.offGenerator.rand();

        }
        return pauseTime;
    }

    public String xml() {
        String xml = "<BURST>" +
                "<PAUSE>" +
                this.pauseGenerator.toString() +
                "</PAUSE>" +
                "<ON>" +
                this.onGenerator.toString() +
                "</ON>" +
                "<OFF>" +
                this.offGenerator.toString() +
                "</OFF>" +
                "</BURST>";
        return xml;


    }

    public void setParameters(HashMap parameters) {
        GeneratorModel genModel = (GeneratorModel) parameters.get("interPacketTime");
        if (parameters.get("samples") != null) {
            genModel.setParameter("samples", parameters.get("samples"));
        } else if (parameters.get("duration") != null) {
            genModel.setParameter("duration", parameters.get("duration"));
        } else {
            genModel.setParameter("numPackets", parameters.get("numPackets"));
        }
        pauseGenerator = (InterPacketTimeGenerator) genModel.getGenerator();
        genModel = (GeneratorModel) parameters.get("offPeriod");
        offGenerator = (RandomGenerator) genModel.getGenerator();
        genModel = (GeneratorModel) parameters.get("onPeriod");
        onGenerator = (RandomGenerator) genModel.getGenerator();
    }
}
